package com.mdxq.mall.product.service.impl;

import com.mdxq.mall.product.entity.*;
import com.mdxq.mall.product.mapper.*;
import com.mdxq.mall.product.service.ProductService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * 商品信息 服务层实现。
 *
 * @author ShuJie
 * @since 2025-01-12
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private AlbumPicMapper albumPicMapper;//商品图册图片
    @Autowired
    private AlbumMapper albumMapper;//商品图册
    @Autowired
    private ProductAttributeValueMapper valueMapper;//属性值
    @Autowired
    private SkuStockMapper skuStockMapper;//sku库存值

    @Override
    @Transactional
    public boolean save(Product entity) {
        mapper.insert(entity);
        Album album = new Album();
        album.setName(entity.getName());//相册名称和商品名称一致
        album.setCoverPic(entity.getPic());//相册封面和商品封面一致
        album.setPicCount(entity.getAlbumPics().split(",").length);
        albumMapper.insert(album);
        // 保存图册中的图片
        for (String s : entity.getAlbumPics().split(",")) {
            AlbumPic albumPic = new AlbumPic(null, album.getId(), s);
            albumPicMapper.insert(albumPic);//保存图册中的图片
        }
        for (ProductAttributeValue attrValue : entity.getProductAttributeValueList()) {
            attrValue.setProductId(entity.getId());
            valueMapper.insert(attrValue);
        }
        for (SkuStock skuStock : entity.getSkuStockList()) {
            skuStock.setProductId(entity.getId());
            skuStockMapper.insert(skuStock);
        }
        return true;
    }

    @Override
    @Transactional
    public boolean updateById(Product entity) {
        mapper.update(entity);
        Album album = new Album();
        album.setName(entity.getName());//相册名称和商品名称一致
        album.setCoverPic(entity.getPic());//相册封面和商品封面一致
        album.setPicCount(entity.getAlbumPics().split(",").length);
        albumMapper.insert(album);
        // 保存图册中的图片
        for (String s : entity.getAlbumPics().split(",")) {
            AlbumPic albumPic = new AlbumPic(null, album.getId(), s);
            albumPicMapper.insert(albumPic);//保存图册中的图片
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id", entity.getId());
        valueMapper.deleteByQuery(queryWrapper);
        for (ProductAttributeValue attrValue : entity.getProductAttributeValueList()) {
            attrValue.setProductId(entity.getId());
            valueMapper.insert(attrValue);
        }
        queryWrapper.clear();
        queryWrapper.eq("product_id", entity.getId());
        skuStockMapper.deleteByQuery(queryWrapper);
        for (SkuStock skuStock : entity.getSkuStockList()) {
            skuStock.setProductId(entity.getId());
            skuStockMapper.insert(skuStock);
        }
        return true;
    }

    @Override
    public Product getById(Serializable id) {
        Product product = super.getById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id", id);
        product.setProductAttributeValueList(valueMapper.selectListByQuery(queryWrapper));
        queryWrapper.clear();
        queryWrapper.eq("product_id", id);
        product.setSkuStockList(skuStockMapper.selectListByQuery(queryWrapper));
        return product;
    }

    @Override
    public boolean updateDeleteStatus(String ids, Integer deleteStatus) {
        for (String s : ids.split(",")) {
            Product product = mapper.selectOneById(Long.valueOf(s));
            product.setDeleteStatus(deleteStatus);
            mapper.update(product);
        }
        return true;
    }
}
